﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.DAO;
using www.Core.Contract.DataContract;
using www.Data.BO;
using www.Models;

namespace www.Controllerss
{
    public class PhotoController
    {
        #region value
        private readonly PhotoReBO _photoReBo = new PhotoReBO();
        private readonly PhotoValueBO _photoValueBo = new PhotoValueBO();
        #endregion

        #region ham lay du lieu

        public IQueryable<RelaxPhotoDataContract> GePhotoDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _photoReBo.GetPhotoDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
       
        #endregion

        #region Add
        public long Add(List<RelaxPhotoDataContract> photoDataContracts)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var story = new PHOTO_REF
                {
                    date = photoDataContracts[0].Date,
                    priority = photoDataContracts[0].Priority,
                    categoryId = Convert.ToInt32(photoDataContracts[0].CategoryId),
                    isdefault = photoDataContracts[0].IsDefault,
                    active = photoDataContracts[0].Active,
                    userId = photoDataContracts[0].Userid,
                    view = photoDataContracts[0].View,
                    userbrower = photoDataContracts[0].UserBrower,
                    type = photoDataContracts[0].Type
                };
                _photoReBo.Add(story);
                var first = _photoReBo.GetPhotoDataContract().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = Convert.ToInt32(first.Id);
                    foreach (var item in photoDataContracts)
                    {
                        var newValue = new PHOTO_VALUE
                        {
                            name = item.Name,
                            tag = item.Tag,
                            image = item.Image,
                            detail = item.Detail,
                            languagecode = item.Languagecode,
                            photoid = iD,
                            report = item.Report
                        };
                        _photoValueBo.Add(newValue);
                    }
                }

                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update
        public Boolean Update(List<RelaxPhotoDataContract> photoDataContracts)
        {
            using (var scope = new TransactionScope())
            {

                var story = new PHOTO_REF
                {
                    id = Convert.ToInt32(photoDataContracts[0].Id),
                    date = photoDataContracts[0].Date,
                    priority = photoDataContracts[0].Priority,
                    categoryId = Convert.ToInt32(photoDataContracts[0].CategoryId),
                    isdefault = photoDataContracts[0].IsDefault,
                    active = photoDataContracts[0].Active,
                    view = photoDataContracts[0].View,
                    userId = photoDataContracts[0].Userid,
                    userbrower = photoDataContracts[0].UserBrower,
                    type = photoDataContracts[0].Type
                };
                _photoReBo.Update(story);
                foreach (var item in photoDataContracts)
                {
                    var newValue = new PHOTO_VALUE
                    {
                        photoid = Convert.ToInt32(item.Id),
                        languagecode = item.Languagecode,
                        name = item.Name,
                        tag = item.Tag,
                        image = item.Image,
                        detail = item.Detail,
                        report = item.Report
                    };
                    if (!_photoValueBo.Update(newValue))
                    {
                        _photoValueBo.Add(newValue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        public void UpdateView(long id)
        {
            _photoReBo.UpdateView(id);
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId, long iLanguageCode)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _photoValueBo.Delete(id, iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}